package com.hilihili.service.impl;

import java.util.*;

import com.hilihili.domain.dto.CommentLimitDTO;
import com.hilihili.domain.entity.CommentLikeUser;
import com.hilihili.mapper.CommentLikeUserMapper;
import com.hilihili.util.DateChangeUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hilihili.mapper.CommentMapper;
import com.hilihili.domain.entity.Comment;
import com.hilihili.service.ICommentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 评论Service业务层处理
 *
 * @author xhc
 * @date 2023-11-15
 */
@Service
public class CommentServiceImpl implements ICommentService
{
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CommentLikeUserMapper commentLikeUserMapper;

    @Override
    public Map<String, Object> getCommentChildren(CommentLimitDTO commentLimitDTO) {
        //计算下标 ：offset = (pageNum - 1) * pageSize
        commentLimitDTO.setStartIndex((commentLimitDTO.getStartIndex() - 1) * commentLimitDTO.getMaxCount());
        Map<String,Object> map = new HashMap<>();
        Long total = commentMapper.selectCommentCountByOriginId(commentLimitDTO.getId());
        if (Objects.isNull(total) || total == 0){
            map.put("replyTotalCount",0);
            return map;
        }
        // 运行到这里 说明一定有至少有一个二级评论
        map.put("replyTotalCount",total);
        List<Comment> comments;
        if (commentLimitDTO.getLatest()){// 按时间降序
            comments = commentMapper.selectCommentChildrenPageByLatest(commentLimitDTO);
        }else {
            comments = commentMapper.selectCommentByOriginIdAndStartIndexAndMaxCount(commentLimitDTO);
        }
        // 修改日期格式
        comments.forEach(comment -> {
            comment.setCreatedAtChange(DateChangeUtil.commentDateChange(comment.getCreatedAt()));
        });
        map.put("childrenList",comments);
        return map;
    }

    @Override
    @Transactional
    public int updateLike(CommentLikeUser commentLikeUser) {
        if (Objects.isNull(commentLikeUser.getCommentId())||commentLikeUser.getSetValue() == 0) return 0;
        // 修改点赞数
        int n = commentMapper.updateCommentLoveById(commentLikeUser);
        int m = 0;
        if (commentLikeUser.getSetValue() == 1){
            // 记录用户点赞的评论
            commentLikeUser.setCreateTime(new Date());
            m = commentLikeUserMapper.insertCommentLike(commentLikeUser);
        }else {
            // 删除用户点赞的评论
            m = commentLikeUserMapper.deleteCommentLike(commentLikeUser);
        }
        return n+m;
    }

    @Override
    public List<Comment> selectCommentByVideoIdAndMaxCount(CommentLimitDTO commentLimitDTO) {
        return commentMapper.selectCommentByVideoIdAndMaxCount(commentLimitDTO);
    }

    @Override
    public List<Comment> selectCommentByLatest(CommentLimitDTO commentLimitDTO) {
        return commentMapper.selectCommentByLatest(commentLimitDTO);
    }

    @Override
    public Comment selectIdMax() {
        return commentMapper.selectIdMax();
    }

    @Override
    public Long selectCommentCountByVideoId(Long videoId) {
        return commentMapper.selectCommentCountByVideoId(videoId);
    }

    @Override
    public Long selectCommentCountByVideoIdAndOriginIdIsNull(Long videoId) {
        return commentMapper.selectCommentCountByVideoIdAndOriginIdIsNull(videoId);
    }


    @Override
    public int deleteComment(Long id, Long originId) {
        // 事务
        if(Objects.isNull(originId)){
            // 是一级评论
            List<Long> idList = commentMapper.selectCommentByOriginId(id);
            if (idList.size() == 0){
                return commentMapper.deleteCommentById(id);
            }else {
                Long[] ids = new Long[idList.size()+1];
                for (int i = 0; i < ids.length-1; i++) {
                    ids[i] = idList.get(i);
                }
                ids[idList.size()] = id;
                return commentMapper.deleteCommentByIds(ids);
            }
        }else {
            // 是二级评论
            return commentMapper.deleteCommentById(id);
        }
    }





    /**
     * 查询评论
     *
     * @param id 评论主键
     * @return 评论
     */
    @Override
    public Comment selectCommentById(Long id)
    {
        return commentMapper.selectCommentById(id);
    }

    /**
     * 查询评论列表
     *
     * @param comment 评论
     * @return 评论
     */
    @Override
    public List<Comment> selectCommentList(Comment comment)
    {
        return commentMapper.selectCommentList(comment);
    }

    /**
     * 新增评论
     *
     * @param comment 评论
     * @return 结果
     */
    @Override
    public int insertComment(Comment comment)
    {
        return commentMapper.insertComment(comment);
    }

    /**
     * 修改评论
     *
     * @param comment 评论
     * @return 结果
     */
    @Override
    public int updateComment(Comment comment)
    {
        return commentMapper.updateComment(comment);
    }

    /**
     * 批量删除评论
     *
     * @param ids 需要删除的评论主键
     * @return 结果
     */
    @Override
    public int deleteCommentByIds(Long[] ids)
    {
        return commentMapper.deleteCommentByIds(ids);
    }

    /**
     * 删除评论信息
     *
     * @param id 评论主键
     * @return 结果
     */
    @Override
    public int deleteCommentById(Long id)
    {
        return commentMapper.deleteCommentById(id);
    }
}
